Ext.define('App.datasource.field.TableFieldGridField', {
    extend: 'Extend.form.field.GridField',
    alias: ['widget.TableFieldGridField'],
    border: false,
    requires: ['App.datasource.field.DataTypeComboBox', 'App.datasource.model.TableFieldModel'],
    model: 'App.datasource.model.TableFieldModel',
    submitFields: ['id'],
    roweditable: true,
    sortableColumns: false,
    userCls: 'high-line28',
    columns: [{
        text: 'ID',
        dataIndex: 'id',
        hidden: true,
        sortable: false
    }, {
        text: '字段名称',
        dataIndex: 'alias',
        editor: {maxLength: 128},
        flex: 4
    }, {
        text: '字段',
        dataIndex: 'fieldName',
        editor: {allowBlank: false, blankText: "字段为必填字段，不能为空！", maxLength: 128},
        flex: 4
    }, {
        text: '数据类型',
        dataIndex: 'dataType',
        editor: {xtype: 'DataTypeComboBox', allowBlank: false, blankText: "数据类型为必填字段，不能为空！"},
        flex: 2
    }, {
        text: '只读',
        dataIndex: 'readOnly',
        flex: 1,
        editor: {xtype: 'booleancombobox'},
        renderer: function (v, m, r) {
            return v == true ? '<span style="color: red;">是</span>' : '否'
        }
    }, {
        text: '关联KPI',
        dataIndex: 'id',
        flex: 1,
        renderer: function (v, m, r) {
            return '<a onclick="javascript:;" href="javascript:;">查看</a>';
        }
    }],
    listeners: {
        beforeedit: function (editor, context) {
            if (context.record.get('readOnly') == true && !app.hasRole('系统管理员')) {
                Extend.Msg.error('错误', "当前字段为只读字段，不允许修改！");
                return false;
            }
            return true;
        },
        cellclick: function ($this, td, cellIndex, record, tr, rowIndex, e, eOpts) {
            var me = this;
            if (td.textContent == '查看') {
                if (record.persistent != false) {
                    if (e.target.localName == 'a') {
                        Ext.create('App.datasource.field.ReferencedKpiWindow', {extraParams: {'params.tableFields.id_eq': record.get('id')}}).show();
                    }
                }
            }
        }
    }
})


Ext.define('App.datasource.field.ReferencedKpiWindow', {
    extend: 'Extend.window.ModalWindow',
    requires: ["App.kpiconfiguration.view.KpiConfigurationList"],
    alias: ['widget:ReferencedKpiWindow'],
    width: 900,
    height: 480,
    items: [{
        xtype: 'KpiConfigurationList',
        autoLoad: false,
        region: 'center',
        tbar: {
            add: {hidden: true},
            remove: {hidden: true}
        },
        listeners: {
            beforeload: function () {
                var me = this;
                var proxy = me.getStore().getProxy();
                proxy.setUrl('kpiconfiguration/query_distinct_sample');
                var extraParams = proxy.getExtraParams();
                Ext.apply(extraParams, me.up().extraParams);
            }
        }
    }]
})